var swfu;

var Uploader = {
	getFileSize: function(num) {
		if (isNaN(num)) {
			return false;
		}
		num = parseInt(num);
		var units = [ " B", " KB", " MB", " GB" ];
		for ( var i = 0; i < units.length; i += 1) {
			if (num < 1024) {
				num = num + "";
				if (num.indexOf(".") != -1 && num.indexOf(".") != 3) {
					num = num.substring(0, 4);
				} else {
					num = num.substring(0, 3);
				}
				break;
			} else {
				num = num / 1024;
			}
		}
		return num + units[i];
	},
	
	updateStatus: function(fileId, html) {
		//$("#file_tr_"+fileId).get(0).cells[2].innerHTML = html;
	},
	
	updateOpt: function(fileId, html) {
		//$("#file_tr_"+fileId).get(0).cells[3].innerHTML = html;
	},
	
	Handler: {
		fileQueued: function(file) {
			//$("#file_list").append("<tr id='file_tr_"+file.id+"'><td>"+file.name+"</td><td>"+Uploader.getFileSize(file.size)+"</td><td>等候上传</td><td class='opt'><a href='#' onclick=deleteFile('"+file.id+"') >删除</a></td></tr>");
			sendFile();
		},
		
		uploadComplete: function(file) {
			//递归实现自动批量上传
			this.startUpload();
		},
		
		uploadStart: function(file) {
			//开始上传此文件
			//Uploader.updateStatus(file.id,"开始上传");
		},
		
		uploadProgress: function(file, bytesLoaded, bytesTotal) {
			var percent = Math.ceil((bytesLoaded / bytesTotal) * 100);
			//Uploader.updateStatus(file.id, percent + "%");
		},
		
		uploadSuccess: function(file, serverData) {
			//Uploader.updateStatus(file.id,"上传完毕");
			//var data = eval("("+serverData+")");
			//Uploader.updateOpt(file.id,"<a href='"+data.fileUrl+"' target='_blank'>查看文件</a>");
		},
		
		fileQueueError: function(file, errorCode, message) {
			console.log(file);
			console.log(errorCode);
			console.log(message);
		}
	}
};

$(function(){
	swfu = new SWFUpload({
		//debug:true,
		//这个地方配置时一定要使用绝对路径，否则容易出错
		upload_url : "/upload",             
		flash_url : "/assets/html/swfupload/swfupload.swf",                              //swf影片路径,如果是互联网项目,可以选择网速较快的网站
		file_size_limit : "110 MB",											//上传文件大小
		file_types : "",                      //上传文件格式
		file_types_description : "All Image Files",							//选择文件时的描述,不重要
		file_post_name : "upload",                                           //发送到服务器端的post信息,一般此处配置cookie信息
		post_params:{},
		
		button_placeholder_id : "spanSWFUploadButton",                       //添加文件按钮
		button_width : 60,													//按钮宽度
		button_height : 20,													//按钮长度
		button_text : "<b>发送文件</b>",							//按钮显示内容
		button_text_left_padding : 3,										//按钮显示左边距
		button_text_top_padding : 2,										//按钮显示右边距
		button_cursor : SWFUpload.CURSOR.HAND,
		
		//handler
		file_queued_handler : Uploader.Handler.fileQueued,
		file_queue_error_handler : Uploader.Handler.fileQueueError,
		upload_complete_handler : Uploader.Handler.uploadComplete,
		upload_start_handler : Uploader.Handler.uploadStart,
		upload_progress_handler : Uploader.Handler.uploadProgress,
		upload_success_handler : Uploader.Handler.uploadSuccess
		
	});
})